window.addEvent('domready', function(){
	var form = $('frmSearch');
	
	//pagination
	new Pagination($('ranking_pagination'),{
		onPageChange: function(obj){
			form.offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	//table header untuk sorting
	new SortableTableHeader($('ranking_table').getElement('tr[class:header]'), {
		tableFieldName:['kary_id','kary_nama','rank_point','rank_ranking'],
		onHeaderClick:function(item, fieldName, sort){
			form.orderby.value = fieldName+" "+sort;
			search_cont(form);
		}
	});
	
	search(form);	
	
	var dtp = $$('.datepicker');
	var tmp = [];
	
	//datepicker
	dtp.each(function(item, index){
		var oo = {};
		if (index<=0){
			oo = {timePicker:true, inputOutputFormat:'Y-m-d', format:'F-Y', onClose:function(){
				search(form);
				tmp[1].options.minDate = {date:tmp[0].attachTo.value, format:'Y-m-d'};
				tmp[1].formatMinMaxDates();
			}};
		}
		if (index>0){
			oo = {timePicker:true, inputOutputFormat:'Y-m-d', format:'F-Y',
				minDate:{date:tmp[0].attachTo.value, format:'Y-m-d'}, onClose:function(){
				search(form);
			}};
		} 
		tmp[index] = dtpicker_attach0(item, oo);
	});
});

function search(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('ranking_table_content').set('html', json.table_content);
			
			//rubah jumlah halamannya
			$('ranking_pagination').retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('ranking_table_content').set('html', json.table_content);
		}
	}).send();
}

function do_drillRank(rankID){
	var form = $('frmSearch');
	var str = form.periode1.value + '/' + form.periode2.value
	FBModal_show2(APP.base+'/ajax/drillRank/'+rankID+'/'+str , 'post', "", true, true, null, {
		onSuccess: function(res){
			
		}
	});
}